﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Web;
using GrupyzServices.BdAccess;
using GrupyzServices.Logging;

namespace GrupyzServices.Reports
{
    public class PdfCreator
    {
        public string CreateVoucher(VoucherViewModel voucher)
        {
            try
            {
                Font BigHelvetica = new Font(Font.FontFamily.HELVETICA, 18f, 1);
                Font helvetica = new Font(Font.FontFamily.HELVETICA, 12f);
                Font xsmalArial = FontFactory.GetFont("Arial", 9, BaseColor.BLACK);
                Font smalArial = FontFactory.GetFont("Arial", 10, BaseColor.BLACK);
                Font medHelvetica = new Font(Font.FontFamily.HELVETICA, 14f, 1);
                
                //create document
                Document document = new Document(PageSize.A4);

                string Filename = HttpContext.Current.Server.MapPath("./ficheiros/vouchers/") + voucher.Referencia + ".pdf";

                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Filename, FileMode.Create));
                document.Open();

                //tabela
                PdfPTable table = new PdfPTable(3);
                table.WidthPercentage = 100f;
                table.DefaultCell.Border = 0;

                //celulas

                PdfPCell cell = new PdfPCell(new Phrase("Voucher adquirido no dia " + voucher.DataCriacao.ToShortDateString(), helvetica));
                cell.Colspan = 2;
                cell.Border = 0;
                cell.BorderColorBottom = BaseColor.GRAY;
                cell.BorderWidthBottom = 2f;
                cell.PaddingBottom = 10;
                cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                table.AddCell(cell);

                cell = new PdfPCell(new Phrase("ID Voucher: " + voucher.Referencia, helvetica));
                cell.Border = 0;
                cell.HorizontalAlignment = 2; //0=Left, 1=Centre, 2=Right
                cell.BorderColorBottom = BaseColor.GRAY;
                cell.BorderWidthBottom = 2f;
                cell.PaddingBottom = 10;
                table.AddCell(cell);


                cell = new PdfPCell();
                cell.Border = 0;
                cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                cell.PaddingTop = 10;
                Image logo = Image.GetInstance(HttpContext.Current.Server.MapPath(".") + voucher.Imagem.Replace("~",""));
                cell.Image = logo;
                table.AddCell(cell);



                //Tabela nested
                PdfPTable nested = new PdfPTable(1);


                Chunk c8 = new Chunk("Voucher para " + voucher.NomeParceiro, medHelvetica);
                Phrase p = new Phrase();
                p.Add(c8);
                cell = new PdfPCell(p);
                cell.Border = 0;
                cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                cell.PaddingTop = 10;
                cell.PaddingLeft = 20;
                cell.Colspan = 2;
                nested.AddCell(cell);

                cell = new PdfPCell(new Phrase(voucher.Descricao, helvetica));
                cell.Border = 0;
                cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                cell.PaddingTop = 10;
                cell.PaddingLeft = 20;
                cell.Colspan = 2;
                nested.AddCell(cell);


                cell = new PdfPCell(new Phrase("Valor do voucher: " + voucher.Valor +"€", BigHelvetica));
                cell.Border = 0;
                cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                cell.PaddingTop = 10;
                cell.PaddingLeft = 20;
                cell.Colspan = 2;
                nested.AddCell(cell);

                cell = new PdfPCell(new Phrase(string.Format("Voucher válido de {0} até {1}", voucher.DataCriacao.ToShortDateString(), voucher.Validade.ToShortDateString()), xsmalArial));
                cell.Border = 0;
                cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                cell.PaddingTop = 10;
                cell.PaddingLeft = 20;
                cell.Colspan = 2;
                nested.AddCell(cell);

                PdfPCell nesthousing = new PdfPCell(nested);
                nesthousing.Border = 0;
                nesthousing.Colspan = 2;
                table.AddCell(nesthousing);


                //c8 = new Chunk("Casa da Picanha", helvetica);
                //p = new Phrase();
                //p.Add(c8);
                cell = new PdfPCell();
                cell.AddElement(new Paragraph(voucher.NomeParceiro, smalArial));
                cell.AddElement(new Paragraph(voucher.Morada, smalArial));
                cell.AddElement(new Paragraph(voucher.CodPostal_Localidade, smalArial));
                cell.Border = 0;
                cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                cell.PaddingTop = 5;
                cell.PaddingBottom = 20;
                cell.Colspan = 2;
                table.AddCell(cell);


                cell = new PdfPCell();
                Paragraph parg = new Paragraph("Código de segurança:", xsmalArial);
                parg.Alignment = 2;
                cell.AddElement(parg);
                parg = new Paragraph(voucher.CodSeguranca, xsmalArial);
                parg.Alignment = 2;
                cell.AddElement(parg);
                cell.Border = 0;
                cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                cell.PaddingLeft = 20;
                cell.PaddingTop = 5;
                cell.PaddingBottom = 20;
                table.AddCell(cell);

                document.Add(table);

                //Listas
                PdfPTable tablelist = new PdfPTable(2);
                tablelist.WidthPercentage = 100f;
                tablelist.DefaultCell.Border = 0;
                tablelist.SpacingAfter = 90;

                PdfPCell cellList = new PdfPCell(new Phrase("A oferta", smalArial));
                cellList.Border = 0;
                cellList.BorderColorTop = BaseColor.GRAY;
                cellList.BorderWidthTop = 2f;
                cellList.PaddingTop = 10;
                cellList.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                tablelist.AddCell(cellList);


                cellList = new PdfPCell(new Phrase("Condições", smalArial));
                cellList.Border = 0;
                cellList.BorderColorTop = BaseColor.GRAY;
                cellList.BorderWidthTop = 2f;
                cellList.PaddingTop = 10;
                cellList.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                tablelist.AddCell(cellList);


                List list = new List(List.UNORDERED);
                list.SetListSymbol("\u2022");
                list.IndentationLeft = 30f;

                foreach (var item in voucher.Destaque)
                {
                    list.Add(new ListItem(item, xsmalArial));
                }
                
                cellList = new PdfPCell();
                cellList.Border = 0;
                cellList.AddElement(list);
                cellList.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                tablelist.AddCell(cellList);

                list = new List(List.UNORDERED);
                list.SetListSymbol("\u2022");
                list.IndentationLeft = 30f;

                foreach (var item in voucher.Condicoes)
                {
                    list.Add(new ListItem(item, xsmalArial));
                }

                cellList = new PdfPCell();
                cellList.Border = 0;
                cellList.AddElement(list);
                cellList.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                tablelist.AddCell(cellList);

                document.Add(tablelist);

                PdfPTable tablePar = new PdfPTable(1);
                tablePar.WidthPercentage = 100f;
                tablePar.DefaultCell.Border = 0;

                PdfPCell cellpar = new PdfPCell(new Phrase("Telefone:926724226 e-mail:mail@descontoemgrupo.com", xsmalArial));
                cellpar.Border = 0;
                cellpar.BackgroundColor = BaseColor.LIGHT_GRAY;
                cellpar.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                tablePar.AddCell(cellpar);

                //VerticalPositionMark mark = new LineSeparator(3,100,BaseColor.GRAY,0,6f);
                //document.Add(mark);

                Image logosmall = Image.GetInstance(HttpContext.Current.Server.MapPath("./content/images/logo_voucher.png"));
                logosmall.ScaleAbsolute(60, 51);
                logosmall.Alignment = Image.ALIGN_CENTER;
                logosmall.SpacingAfter = 5;
                logosmall.SpacingBefore = 20;
                document.Add(logosmall);

                document.Add(tablePar);

                Paragraph par = new Paragraph("Obrigado por usar DescontoEmGrupo.com!", xsmalArial);
                par.SpacingBefore = 20;
                par.Alignment = 1;
                document.Add(par);

                par = new Paragraph("Em caso de alguma dúvida ou esclarecimento contacte-nos.", xsmalArial);
                //par.SpacingBefore = 20;
                par.Alignment = 1;
                document.Add(par);

                document.Close();
                return Filename;
            }
            catch (Exception)
            {
                return null;
            }
            
        }


    }
}
